<?php

namespace app\admin\controller;

class Activities extends Home
{
    protected static $activitiesModel;

    public function __construct()
    {
        parent::__construct();
        self::$activitiesModel = new \app\common\model\Activities();
    }

    /**
     * 活动列表
     * @return mixed
     */
    public function lists()
    {
        $list = self::$activitiesModel->order('id asc')->select();
        if ($list) {
            foreach ($list as $val) {
                $val->bonus_num = \app\common\model\ActivityMembers::where('activity_id', $val['id'])->where('status', 1)->count();
            }
        }

        $this->assign('_list', $list);
        $this->assign('title', '活动列表');
        return $this->fetch();
    }

    /**
     * 获取活动信息
     * @return mixed
     */
    public function one()
    {
        $id = input('id');
        $rowInfo = [];
        if ($id > 0) {
            $rowInfo = self::$activitiesModel->where('id', $id)->find();
        }

        $this->assign('row_info', $rowInfo);
        $this->assign('title', '活动信息');
        return $this->fetch();
    }

    /**
     * 新增活动
     * @return mixed
     */
    public function addOredit()
    {
        $data = request()->post();
        $id = $data['id'];

        if ($id) {
            $ainfo = \app\common\model\Activities::get($id);
            $ainfo->start_time = $data['start_time'];
            $ainfo->end_time = $data['end_time'];
            $ainfo->period = $data['period'];
            $ainfo->total_num = $data['total_num'];
            $res = $ainfo->save();
        } else {
            self::$activitiesModel->start_time = $data['start_time'];
            self::$activitiesModel->end_time = $data['end_time'];
            self::$activitiesModel->period = $data['period'];
            self::$activitiesModel->total_num = $data['total_num'];
            $res = self::$activitiesModel->save();
        }

        if ($res) {
            $this->success('操作成功！', url('lists'));
        } else {
            $this->error('操作失败！');
        }
    }

    /**
     * 删除活动
     */
    public function del()
    {
        $ids = input('post.id');
        if (!is_array($ids)) {
            $ids = (array)$ids;
        }

        $res = self::$activitiesModel->where('id', 'in', $ids)->delete();
        if ($res) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}